package com.itheima.ums.controller;

import cn.hutool.core.collection.CollUtil;
import com.itheima.mall.pojo.Menu;
import com.itheima.mall.pojo.Resource;
import com.itheima.mall.pojo.Role;
import com.itheima.mall.vo.PageResult;
import com.itheima.ums.service.UmsRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
public class UmsRoleController {

    @Autowired
    private UmsRoleService umsRoleService;

    /**
     * 根据角色名称分页获取角色列表
     *
     * @param pageNum
     * @param pageSize
     * @param keyword
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity selectRolePage(@RequestParam(defaultValue = "1") Integer pageNum,
                                         @RequestParam(defaultValue = "10") Integer pageSize,
                                         String keyword) {
        PageResult pageResult = umsRoleService.list(pageNum, pageSize, keyword);
        if (pageResult == null) {
            return ResponseEntity.ok("ok");
        }
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 查询全部角色信息
     *
     * @return
     */
    @GetMapping("/listAll")
    public ResponseEntity selectRoleAll() {
        List<Role> list = umsRoleService.listAll();
        if (CollUtil.isEmpty(list)) {
            return ResponseEntity.ok("ok");
        }
        return ResponseEntity.ok(list);
    }

    /**
     * 根据角色id查询目录信息
     *
     * @param roleId
     * @return
     */
    @GetMapping("/listMenu/{roleId}")
    public ResponseEntity getMenuListById(@PathVariable("roleId") Long roleId) {
        List<Menu> list = umsRoleService.getMenuListById(roleId);
        if (CollUtil.isEmpty(list)) {
            return ResponseEntity.ok("ok");
        }
        return ResponseEntity.ok(list);
    }

    /**
     * 获取角色相关资源
     *
     * @param roleId
     * @return
     */
    @GetMapping("/listResource/{roleId}")
    public ResponseEntity listResource(@PathVariable("roleId") Long roleId) {
        List<Resource> list = umsRoleService.listResource(roleId);
        if (CollUtil.isEmpty(list)) {
            return ResponseEntity.ok("ok");
        }
        return ResponseEntity.ok(list);
    }

    /**
     * 给角色分配菜单
     *
     * @param roleId
     * @param menuIds
     * @return
     */
    @PostMapping("/allocMenu")
    public ResponseEntity allocMenu(Long roleId, @RequestParam List<Integer> menuIds) {
        Integer count = umsRoleService.allocMenu(roleId, menuIds);
        if (count <= 0) {
            return ResponseEntity.ok("ok");
        }
        return ResponseEntity.ok(count);
    }

    /**
     * 给角色分配资源
     *
     * @param roleId
     * @param resourceIds
     * @return
     */
    @PostMapping("/allocResource")
    public ResponseEntity allocResource(Long roleId, @RequestParam List<Integer> resourceIds) {
        Integer count = umsRoleService.allocResource(roleId, resourceIds);
        if (count <= 0) {
            return ResponseEntity.ok("ok");
        }
        return ResponseEntity.ok(count);
    }

    /**
     * 添加角色
     *
     * @param role
     * @return
     */
    @PostMapping("/create")
    public ResponseEntity createRole(@RequestBody Role role) {
        Integer count = umsRoleService.createRole(role);
        if (count <= 0) {
            return ResponseEntity.ok("ok");
        }
        return ResponseEntity.ok(count);
    }

    /**
     * 批量删除角色
     *
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    public ResponseEntity deleteByIds(@RequestParam List<Long> ids) {

        Integer count = umsRoleService.deleteByIds(ids);
        if (count <= 0) {
            return ResponseEntity.ok("ok");
        }
        return ResponseEntity.ok(count);
    }

    /**
     * 修改角色
     *
     * @param role
     * @param id
     * @return
     */
    @PostMapping("/update/{id}")
    public ResponseEntity updateById(@RequestBody Role role, @PathVariable("id") Long id) {
        Integer count = umsRoleService.updateById(role, id);
        if (count <= 0) {
            return ResponseEntity.ok("ok");
        }
        return ResponseEntity.ok(count);
    }

    /**
     * 修改角色状态
     *
     * @param id
     * @param status
     * @return
     */
    @PostMapping("/updateStatus/{id}")
    public ResponseEntity updateStatus(@PathVariable("id") Long id,
                                       @RequestParam(name = "status") Long status) {
        Integer count = umsRoleService.updateStatus(id, status);
        if (count <= 0) {
            return ResponseEntity.ok("ok");
        }

        return ResponseEntity.ok(count);

    }
}